import time
import requests
import json
# import os



class WeChatSMSAlarm:
    def __init__(self):
        self.CORPID = 'xx'  # 企业ID,登陆企业微信，在我的企业-->企业信息里查看
        self.CORPSECRET = 'xx'  # 自建应用，每个自建应用里都有单独的secret
        self.AGENTID = 'xx'  # 应用代码
        self.TOUSER = "@all"  # @all 全体成员
        self.TOPARY = "1"  # 部门ID

    def _get_access_token(self):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
        values = {'corpid': self.CORPID, 'corpsecret': self.CORPSECRET, }
        req = requests.post(url, params=values)
        data = json.loads(req.text)
        # print (data)
        return data["access_token"]

    def get_access_token(self):
        try:
            with open('access_token.conf', 'r') as f:
                t, access_token = f.read().split()
        except BaseException:
            with open('access_token.conf', 'w') as f:
                access_token = self._get_access_token()
                cur_time = time.time()
                f.write('\t'.join([str(cur_time), access_token]))
                return access_token
        else:
            cur_time = time.time()
            if 0 < cur_time - float(t) < 7200:
                return access_token
            else:
                with open('access_token.conf', 'w') as f:
                    access_token = self._get_access_token()
                    f.write('\t'.join([str(cur_time), access_token]))
                    return access_token

    def send_data(self, msg):
        send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + \
                   self.get_access_token()
        send_values = {
            "touser": self.TOUSER,
            "msgtype": "text",
            "agentid": self.AGENTID,
            "text": {
                "content": msg
            },
            "safe": "0"
        }
        send_msges = (bytes(json.dumps(send_values), 'utf-8'))
        respone = requests.post(send_url, send_msges)
        respone = respone.json()
        return respone["errmsg"]


if __name__ == '__main__':
    wx = WeChatSMSAlarm()
    wx.send_data(msg="服务都挂了,速度来嗨!")
